#include "wx/wxprec.h"
#ifndef WXPRECOMP
#include "wx/object.h"
#include "wx/dynarray.h"
#include "wx/intl.h"
#include "wx/log.h"
#include "wx/stream.h"
#include "wx/sizer.h"

#include "wx/bmpbuttn.h"
#include "wx/statbox.h"
#include "wx/radiobox.h"
#include "wx/menu.h"
#include "wx/settings.h"
#include "wx/msgdlg.h"
#include "wx/textctrl.h"
#include "wx/toolbar.h"
#include "wx/choicdlg.h"
#include "wx/filedlg.h"
#endif // WXPRECOMP

#include "wx/html/helpfrm.h"
#include "wx/html/helpctrl.h"
#include "wx/notebook.h"
#include "wx/imaglist.h"
#include "wx/treectrl.h"
#include "wx/tokenzr.h"
#include "wx/wfstream.h"
#include "wx/html/htmlwin.h"
#include "wx/busyinfo.h"
#include "wx/progdlg.h"
#include "wx/fontenum.h"
#include "wx/artprov.h"
#include "wx/spinctrl.h"

IMPLEMENT_DYNAMIC_CLASS( wxHtmlHelpFrame, wxFrame )

BEGIN_EVENT_TABLE( wxHtmlHelpFrame, wxFrame )
  EVT_ACTIVATE( wxHtmlHelpFrame::OnActivate )
  EVT_CLOSE( wxHtmlHelpFrame::OnCloseWindow )
END_EVENT_TABLE()

wxHtmlHelpFrame::wxHtmlHelpFrame( wxWindow* parent, wxWindowID id, const wxString& title,
                                  int style, wxHtmlHelpData* data,
                                  wxConfigBase *config, const wxString& rootpath ) {
  Init( data );
  Create( parent, id, title, style, config, rootpath );
}

void wxHtmlHelpFrame::Init( wxHtmlHelpData* data ) {
  // Simply pass the pointer on to the help window
  m_Data = data;
  m_HtmlHelpWin = NULL;
  m_helpController = ( wxHtmlHelpController* ) NULL;
}

// Create: builds the GUI components.
bool wxHtmlHelpFrame::Create( wxWindow* parent, wxWindowID id,
                              const wxString& ( title ), int style,
                              wxConfigBase *config, const wxString& rootpath ) {
  m_HtmlHelpWin = new wxHtmlHelpWindow( m_Data );
  m_HtmlHelpWin->SetController( m_helpController );
  if( config ) {
    m_HtmlHelpWin->UseConfig( config, rootpath );
  }
  wxFrame::Create( parent, id, _( "Help" ),
                   wxPoint( m_HtmlHelpWin->GetCfgData().x, m_HtmlHelpWin->GetCfgData().y ),
                   wxSize( m_HtmlHelpWin->GetCfgData().w, m_HtmlHelpWin->GetCfgData().h ),
                   wxDEFAULT_FRAME_STYLE, wxT( "wxHtmlHelp" ) );
  #if wxUSE_STATUSBAR
  CreateStatusBar();
  #endif
  m_HtmlHelpWin->Create( this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
                         wxTAB_TRAVERSAL | wxNO_BORDER, style );
  GetPosition( & ( m_HtmlHelpWin->GetCfgData().x ), & ( m_HtmlHelpWin->GetCfgData() ).y );
  SetIcon( wxArtProvider::GetIcon( wxART_HELP, wxART_FRAME_ICON ) );
  m_HtmlHelpWin->GetHtmlWindow()->SetRelatedFrame( this, m_TitleFormat );
  #if wxUSE_STATUSBAR
  m_HtmlHelpWin->GetHtmlWindow()->SetRelatedStatusBar( 0 );
  #endif
  return true;
}

wxHtmlHelpFrame::~wxHtmlHelpFrame() {
}

void wxHtmlHelpFrame::SetTitleFormat( const wxString& format ) {
  if( GetHelpWindow() && GetHelpWindow()->GetHtmlWindow() ) {
    GetHelpWindow()->GetHtmlWindow()->SetRelatedFrame( this, format );
  }
  m_TitleFormat = format;
}

/*
  EVENT HANDLING :
*/


void wxHtmlHelpFrame::OnActivate( wxActivateEvent& event ) {
  if( event.GetActive() && m_HtmlHelpWin ) {
    m_HtmlHelpWin->GetHtmlWindow()->SetFocus();
  }
  event.Skip();
}

void wxHtmlHelpFrame::OnCloseWindow( wxCloseEvent& evt ) {
  if( !IsIconized() ) {
    GetSize( & ( m_HtmlHelpWin->GetCfgData().w ), &( m_HtmlHelpWin->GetCfgData().h ) );
    GetPosition( & ( m_HtmlHelpWin->GetCfgData().x ), & ( m_HtmlHelpWin->GetCfgData().y ) );
  }
  if( IsGrabbed() ) {
    RemoveGrab();
  }
  if( m_HtmlHelpWin->GetSplitterWindow() && m_HtmlHelpWin->GetCfgData().navig_on ) {
    m_HtmlHelpWin->GetCfgData().sashpos = m_HtmlHelpWin->GetSplitterWindow()->GetSashPosition();
  }
  if( m_helpController && m_helpController->IsKindOf( CLASSINFO( wxHtmlHelpController ) ) ) {
    ( ( wxHtmlHelpController* ) m_helpController )->OnCloseFrame( evt );
  }
  evt.Skip();
}

void wxHtmlHelpFrame::AddGrabIfNeeded() {
  bool needGrab = false;
  for( wxWindowList::iterator it = wxTopLevelWindows.begin();
       it != wxTopLevelWindows.end();
       ++it ) {
    wxWindow *win = *it;
    wxDialog *dialog = wxDynamicCast( win, wxDialog );
    if( dialog && dialog->IsModal() ) {
      needGrab = true;
    }
  }
  if( needGrab ) {
    AddGrab();
  }
}

void wxHtmlHelpFrame::UseConfig( wxConfigBase *config, const wxString& rootPath ) {
  if( m_HtmlHelpWin ) {
    m_HtmlHelpWin->UseConfig( config, rootPath );
  }
}
